﻿namespace PerpetuumSoft.InstrumentationPDA
{
    using PerpetuumSoft.InstrumentationPDA.Mathematics;
    using System;
    using System.Collections;
    using System.Drawing;

    public class DigitsDisplay
    {
        private Color activeColor;
        private static Hashtable CharSegments = new Hashtable();
        private int digitHeight;
        private int digitSpace;
        private int digitWidth;
        private Color inactiveColor;
        private Vector[][] segmentPaths;
        private int segmentSpace;
        private int segmentThickness;

        static DigitsDisplay()
        {
            CharSegments.Add('0', 0x3f);
            CharSegments.Add('1', Segments.RightBottom | Segments.RightTop);
            CharSegments.Add('2', 0x5b);
            CharSegments.Add('3', 0x6b);
            CharSegments.Add('4', 0x6c);
            CharSegments.Add('5', 0x67);
            CharSegments.Add('6', 0x77);
            CharSegments.Add('7', Segments.RightBottom | Segments.RightTop | Segments.Top);
            CharSegments.Add('8', Segments.All);
            CharSegments.Add('9', 0x6f);
            CharSegments.Add(' ', Segments.None);
            CharSegments.Add('-', Segments.Middle);
        }

        public DigitsDisplay()
        {
            this.digitWidth = 12;
            this.digitHeight = 0x18;
            this.digitSpace = 3;
            this.segmentThickness = 2;
            this.segmentSpace = 1;
            this.activeColor = Color.Lime;
            this.inactiveColor = Color.DarkGreen;
        }

        public DigitsDisplay(Color activeColor, int digitHeight, int digitSpace, int digitWidth, Color inactiveColor, int segmentSpace, int segmentThickness)
        {
            this.digitWidth = 12;
            this.digitHeight = 0x18;
            this.digitSpace = 3;
            this.segmentThickness = 2;
            this.segmentSpace = 1;
            this.activeColor = Color.Lime;
            this.inactiveColor = Color.DarkGreen;
            this.activeColor = activeColor;
            this.digitHeight = digitHeight;
            this.digitSpace = digitSpace;
            this.digitWidth = digitWidth;
            this.inactiveColor = inactiveColor;
            this.segmentSpace = segmentSpace;
            this.SegmentThickness = segmentThickness;
        }

        private void AddSegmentRegion(int num, Vector[] path)
        {
        }

        private void CalculateRegions()
        {
            if (this.segmentPaths == null)
            {
                this.segmentPaths = new Vector[7][];
                decimal y = this.digitHeight / 2;
                decimal num2 = this.segmentThickness / 2;
                this.segmentPaths[0] = new Vector[] { new Vector(this.segmentSpace, 0M), new Vector(this.digitWidth - this.segmentSpace, 0M), new Vector((this.digitWidth - this.segmentSpace) - this.segmentThickness, this.segmentThickness), new Vector(this.segmentSpace + this.segmentThickness, this.segmentThickness) };
                this.segmentPaths[1] = new Vector[] { new Vector(this.segmentSpace, this.digitHeight), new Vector(this.digitWidth - this.segmentSpace, this.digitHeight), new Vector((this.digitWidth - this.segmentSpace) - this.segmentThickness, this.digitHeight - this.segmentThickness), new Vector(this.segmentSpace + this.segmentThickness, this.digitHeight - this.segmentThickness) };
                this.segmentPaths[2] = new Vector[] { new Vector(0M, this.segmentSpace), new Vector(0M, y - this.segmentSpace), new Vector(this.segmentThickness, (y - this.segmentSpace) - this.segmentThickness), new Vector(this.segmentThickness, this.segmentSpace + this.segmentThickness) };
                this.segmentPaths[3] = new Vector[] { new Vector(this.digitWidth, this.segmentSpace), new Vector(this.digitWidth, y - this.segmentSpace), new Vector(this.digitWidth - this.segmentThickness, (y - this.segmentSpace) - this.segmentThickness), new Vector(this.digitWidth - this.segmentThickness, this.segmentSpace + this.segmentThickness) };
                this.segmentPaths[4] = new Vector[] { new Vector(0M, this.digitHeight - this.segmentSpace), new Vector(0M, (this.digitHeight - y) + this.segmentSpace), new Vector(this.segmentThickness, ((this.digitHeight - y) + this.segmentSpace) + this.segmentThickness), new Vector(this.segmentThickness, (this.digitHeight - this.segmentSpace) - this.segmentThickness) };
                this.segmentPaths[5] = new Vector[] { new Vector(this.digitWidth, this.digitHeight - this.segmentSpace), new Vector(this.digitWidth, (this.digitHeight - y) + this.segmentSpace), new Vector(this.digitWidth - this.segmentThickness, ((this.digitHeight - y) + this.segmentSpace) + this.segmentThickness), new Vector(this.digitWidth - this.segmentThickness, (this.digitHeight - this.segmentSpace) - this.segmentThickness) };
                this.segmentPaths[6] = new Vector[] { new Vector(this.segmentSpace, y), new Vector(this.segmentSpace + num2, y - num2), new Vector((this.digitWidth - this.segmentSpace) - num2, y - num2), new Vector(this.digitWidth - this.segmentSpace, y), new Vector((this.digitWidth - this.segmentSpace) - num2, y + num2), new Vector(this.segmentSpace + num2, y + num2) };
            }
        }

        private void ClearRegions()
        {
            this.segmentPaths = null;
        }

        private void PaintDigit(Graphics g, Vector pos, Segments digit)
        {
            Brush brush = new SolidBrush(this.activeColor);
            Brush brush2 = new SolidBrush(this.inactiveColor);
            int num = (int) digit;
            for (int i = 0; i < 7; i++)
            {
                Point[] points = new Point[this.segmentPaths[i].Length];
                for (int j = 0; j < points.Length; j++)
                {
                    points[j].X = (int) Math.Round((decimal) (this.segmentPaths[i][j].X + pos.X), 0);
                    points[j].Y = (int) Math.Round((decimal) (this.segmentPaths[i][j].Y + pos.Y), 0);
                }
                if (this.segmentPaths[i] != null)
                {
                    Brush brush3;
                    if ((num & 1) == 1)
                    {
                        brush3 = brush;
                    }
                    else
                    {
                        brush3 = brush2;
                    }
                    g.FillPolygon(brush3, points);
                    num = num >> 1;
                }
            }
        }

        public void PaintString(Graphics g, Vector pos, string text)
        {
            this.CalculateRegions();
            Brush brush = new SolidBrush(this.activeColor);
            new SolidBrush(this.inactiveColor);
            foreach (char ch in text)
            {
                if (CharSegments.ContainsKey(ch))
                {
                    this.PaintDigit(g, pos, (Segments) CharSegments[ch]);
                    pos.X += this.digitSpace + this.digitWidth;
                }
                else if (ch == ':')
                {
                    g.FillRectangle(brush, (int) Math.Round(pos.X, 0), (int) Math.Round((decimal) (pos.Y + ((this.digitHeight * 3) / 10)), 0), (int) Math.Round((decimal) this.segmentThickness, 0), (int) Math.Round((decimal) this.segmentThickness, 0));
                    g.FillRectangle(brush, (int) Math.Round(pos.X, 0), (int) Math.Round((decimal) (pos.Y + ((this.digitHeight * 7) / 10)), 0), (int) Math.Round((decimal) this.segmentThickness, 0), (int) Math.Round((decimal) this.segmentThickness, 0));
                    pos.X += this.digitSpace + this.SegmentThickness;
                }
                else if ((ch == '.') || (ch == ','))
                {
                    g.FillRectangle(brush, (int) Math.Round(pos.X, 0), (int) Math.Round((decimal) (pos.Y + this.digitHeight), 0), (int) Math.Round((decimal) this.segmentThickness, 0), (int) Math.Round((decimal) this.segmentThickness, 0));
                    pos.X += this.digitSpace + this.SegmentThickness;
                }
            }
        }

        public Color ActiveColor
        {
            get
            {
                return this.activeColor;
            }
            set
            {
                if (this.activeColor != value)
                {
                    this.activeColor = value;
                }
            }
        }

        public int DigitHeight
        {
            get
            {
                return this.digitHeight;
            }
            set
            {
                if (this.digitHeight != value)
                {
                    this.digitHeight = value;
                    this.ClearRegions();
                }
            }
        }

        public int DigitSpace
        {
            get
            {
                return this.digitSpace;
            }
            set
            {
                if (this.digitSpace != value)
                {
                    this.digitSpace = value;
                }
            }
        }

        public int DigitWidth
        {
            get
            {
                return this.digitWidth;
            }
            set
            {
                if (this.digitWidth != value)
                {
                    this.digitWidth = value;
                    this.ClearRegions();
                }
            }
        }

        public Color InactiveColor
        {
            get
            {
                return this.inactiveColor;
            }
            set
            {
                if (this.inactiveColor != value)
                {
                    this.inactiveColor = value;
                }
            }
        }

        public int SegmentSpace
        {
            get
            {
                return this.segmentSpace;
            }
            set
            {
                if (this.segmentSpace != value)
                {
                    this.segmentSpace = value;
                    this.ClearRegions();
                }
            }
        }

        public int SegmentThickness
        {
            get
            {
                return this.segmentThickness;
            }
            set
            {
                if (this.segmentThickness != value)
                {
                    this.segmentThickness = value;
                    this.ClearRegions();
                }
            }
        }

        private enum Segments
        {
            All = 0x7f,
            Bottom = 2,
            LeftBottom = 0x10,
            LeftTop = 4,
            Middle = 0x40,
            None = 0,
            RightBottom = 0x20,
            RightTop = 8,
            Top = 1
        }
    }
}

